﻿using BasicData.API.Write.Application.Commands;
using BasicData.Domain;
using BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BasicData.API.Write.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MangementController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<MangementController> logger;

        public MangementController(IMediator mediator, ILogger<MangementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 测试
        /// </summary>
        /// <returns>返回测试任务</returns>
        [HttpGet]
        public IActionResult Get()
        {
            try
            {
                logger.LogInformation("测试成功");
                int num = int.Parse("ABC");
                return Ok("完成");
            }
            catch (Exception ex)
            {
                logger.LogError("测试失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 全局测试
        /// </summary>
        /// <returns>返回全局测试任务</returns>
        [HttpGet]
        public IActionResult GetLog()
        {
            try
            {
                logger.LogInformation("全局测试成功");
                int num = 0;
                int num1 = 1;
                int num2 = num1 / num;
                return Ok("完成");
            }
            catch (Exception ex)
            {
                logger.LogError("全局测试失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns>返回登录任务</returns>
        [HttpPost]
        public Task<APIResult<int>> CreateGoods(CreateGoodsCommand command)
        {
            try
            {
                logger.LogInformation("添加成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加失败", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns>返回保存任务</returns>
        [HttpPost]
        public Task<APIResult<int>> CreateChan(CreateChanAllCommand command)
        {
            try
            {
                logger.LogInformation("保存成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("保存失败", ex.Message);
                throw;
            }
        }
    }
}
